<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vuex</title>
</head>

<body>
  <div id="app">
    <div @click="add">{{count}}</div>
    <div>第二段{{countP(100)}}</div>
  </div>
  <script src="https://unpkg.com/vue@2"></script>
  <script src="https://unpkg.com/vuex@3"></script>
  <script>
    console.log(Vue, Vuex);
    Vue.use(Vuex)
    let { Store, mapState, mapGetters,mapMutations } = Vuex;
    const store = new Store({
      state: {
        count: 0
      },
      getters: {
        countP: (state) => n => n * n
      },
      mutations: {
        increment(state) {
          state.count++;
        },
        decrement(state) {
          state.count--;
        },
        cat(state,preload){
          console.log(preload);
          preload.num&&(state.count+=preload.num);
        }
      },
    });
    let app = new Vue({
      el: "#app",
      store,
      data() {
        return {
          cat: "白猫"
        }
      },
      computed: {
        ...mapState(["count"]),
        ...mapGetters(['countP'])
      },
      components: {
        WeCom: {
          template: `<div>{{count}}</div>`,
          computed: {
            ...mapState(["count"])
          }
        }
      },
      methods: {
        add() {
          console.log(this);
          this.$store.commit("increment");
        },
        ...mapMutations(["increment","decrement"])
      }
    });
  </script>
</body>

</html>